package com.jdjw.modules.abuttingjoint.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.abuttingjoint.entity.DwdGcJbxxJiande;
import com.jdjw.modules.abuttingjoint.mapper.DwdGcJbxxJiandeMapper;
import com.jdjw.modules.abuttingjoint.service.DwdGcJbxxJiandeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.modules.abuttingjoint.timing.TimedTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 工程-基本信息 服务实现类
 * </p>
 *
 * @author guxinao
 * @since 2021-07-02
 */
@Service
@Slf4j
@DS("sjwjcjdcz")
@Component
public class DwdGcJbxxJiandeServiceImpl extends ServiceImpl<DwdGcJbxxJiandeMapper, DwdGcJbxxJiande> implements DwdGcJbxxJiandeService {

    private static DwdGcJbxxJiandeMapper dwdGcJbxxJiandeMapper;

    @Resource
    public void setDwdGcJbxxJiandeMapper(DwdGcJbxxJiandeMapper dwdGcJbxxJiandeMapper) {
        DwdGcJbxxJiandeServiceImpl.dwdGcJbxxJiandeMapper = dwdGcJbxxJiandeMapper;
    }

    @Override
    public ResponseResult doPostTestTwoo(String time) {
        ResponseResult results = new ResponseResult();
        results.setMsg("测试关闭");
        return results;
    }

    public static void doPostTestTwoo(String time,String dateDq) {
        String url = "http://39.170.77.212:8096/sysn/data/gc-jbxx";
        ArrayList list = new ArrayList();
        ResponseResult results = new ResponseResult();

        try{
            //查询工程-基本信息人员
            QueryWrapper entityWrapper = new QueryWrapper();
            entityWrapper.like("uploadtime", time);
            List<DwdGcJbxxJiande> all = dwdGcJbxxJiandeMapper.selectList(entityWrapper);

            if(all == null || all.size()<1){
                log.info("工程-基本信息：数据库查询数据为0条");
                return;
            }

            for(DwdGcJbxxJiande a:all){
                if(a.getHtbm()!=null){
                    //将数据的上传时间改为推送时间
                    a.setUploadtime(dateDq);
                    //转化为json字符串
                    String jsonStr = JSONObject.toJSONString(a);
                    //转化为json对象
                    list.add(JSONObject.parseObject(jsonStr));
                }
            }

            if(list.size()>0){
                results = TimedTask.getHttpResponseJson(url,list);
            }else {
                log.info("工程-基本信息：数据不全，符合条件的数据为0条");
                return ;
            }

            if(results.getCode().equals(200)){
                log.info("工程-基本信息人员推送成功",results);
                UpdateWrapper<DwdGcJbxxJiande> wrapper = new UpdateWrapper<>();
                wrapper.like("uploadtime",time).set("bx","1");
                int b = dwdGcJbxxJiandeMapper.update(null,wrapper);
            }else {
                log.info("工程-基本信息人员推送失败",results);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    @Override
    public String htbhShengcheng(){
        StringBuilder str = new StringBuilder();
        str.append("jd");
        DwdGcJbxxJiande dwdGcJbxxJiande = new DwdGcJbxxJiande();
        int b = 0;

        //查询
        QueryWrapper<DwdGcJbxxJiande> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","lxrq");
        List<DwdGcJbxxJiande> list = dwdGcJbxxJiandeMapper.selectList(queryWrapper);

        for(DwdGcJbxxJiande a:list){
            if(a.getLxrq()!=null){
                str.append(stringDate(a.getLxrq()));
                str.append(createRandomCharData());
                dwdGcJbxxJiande.setId(a.getId());
                dwdGcJbxxJiande.setHtbm(str.toString());
                int integer = dwdGcJbxxJiandeMapper.updateById(dwdGcJbxxJiande);
                b++;
                str.delete(2,str.length());
            }
        }

        return "ok"+b;
    }


    /**
     * 生成5位英文数字混合随机码
     * @return
     */
    public String createRandomCharData() {
        StringBuilder sb=new StringBuilder();
        Random rand=new Random();//随机用以下三个随机生成器
        Random randdata=new Random();
        int data=0;
        for(int i=0;i<5;i++) {
            int index=rand.nextInt(3);
            //目的是随机选择生成数字，大小写字母
            switch(index) {
                case 0:
                    data=randdata.nextInt(10);//仅仅会生成0~9
                    sb.append(data);
                    break;
                case 1:
                    data=randdata.nextInt(26)+65;//保证只会产生65~90之间的整数
                    sb.append((char)data);
                    break;
                case 2:
                    data=randdata.nextInt(26)+97;//保证只会产生97~122之间的整数
                    sb.append((char)data);
                    break;
            }
        }
        String result = sb.toString();
        return result;
    }


    /**
     * 切割时间字符串
     * 格式：yyyy-MM-dd HH:mm:ss
     */
    public String stringDate(String date){
        String str1 = date.substring(0,4);
        String str2 = date.substring(5,7);
        String str3 = date.substring(8,10);
        return str1+str2+str3;
    }
}
